package com.wangc.commons.result;

import com.wangc.commons.enums.CommonCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 返回结果javaBean
 *
 * @author LoongChow
 * @date 2018/10/22
 */
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RespResult<T> extends Result implements Serializable {

    private static final long serialVersionUID = -8231553940046313154L;

    private String returnStatus;
    private T result;
    private int returnCode;
    private String returnMessage;

    public static <T> RespResult<T> success(String returnMessage) {
        return statusWith(SUCC, null, CommonCodeEnum.SUCCESS.getCode(), returnMessage);
    }

    public static <T> RespResult<T> success(String msg, T result) {
        return statusWith(SUCC, result, CommonCodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> RespResult<T> error(String msg) {
        return RespResult.error(CommonCodeEnum.ERROR.getCode(), msg);
    }

    public static <T> RespResult<T> error(int code, String msg) {
        return statusWith(ERROR, null, code, msg);
    }

    public static <T> RespResult<T> error(int code, T result, String msg) {
        return statusWith(ERROR, result, code, msg);
    }

    public static <T> RespResult<T> statusWith(String returnStatus, T result, int returnCode, String returnMessage) {
        return new RespResult<T>(returnStatus, result, returnCode, returnMessage);
    }

    /**
     * 是否成功
     *
     * @param response
     * @return
     */
    public static boolean isCallSuccess(RespResult<?> response) {
        return response != null && CommonCodeEnum.SUCCESS.getCode() == response.getReturnCode();
    }

}
